#include "tim3.h"
#include "Sys_Config.h"
#if MD_USD_SALVE
//#include "MDS_RTU_Serial.h"
#include "MDS_RTU_Serial_1.h"
#else 
#include "MDM_RTU_Serial.h"
#endif

vu32 sys_tick_100us=0;

//General purpose timer 3 interrupt initialization
//The clock here is 2 times that of APB1, and APB1 is 36M
//arr: automatic reload value.
//psc: clock prescaler number
//Timer 3 is used here!
void TIM3_Int_Init(u16 arr,u16 psc)
{
	TIM_TimeBaseInitTypeDef beep_structure;
	NVIC_InitTypeDef nvic_structure;
	
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM7,ENABLE);
	
	beep_structure.TIM_ClockDivision=TIM_CKD_DIV1;
	beep_structure.TIM_CounterMode=TIM_CounterMode_Up;
	beep_structure.TIM_Period=arr;
	beep_structure.TIM_Prescaler=psc;
	beep_structure.TIM_RepetitionCounter=0;
	
	TIM_TimeBaseInit(TIM7,&beep_structure);
	
	nvic_structure.NVIC_IRQChannel=TIM7_IRQn;
	nvic_structure.NVIC_IRQChannelCmd=ENABLE;
	nvic_structure.NVIC_IRQChannelPreemptionPriority=1;
	nvic_structure.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&nvic_structure);
	TIM_ITConfig(TIM7,TIM_IT_Update,ENABLE);

	TIM_Cmd(TIM7,ENABLE);
}


void TIM7_IRQHandler(void)   												
{
	if (TIM_GetITStatus(TIM7,TIM_IT_Update) != RESET) 	
	{
			TIM_ClearITPendingBit(TIM7,TIM_IT_Update);  		 
		sys_tick_100us++;
		#if !MD_RTU_USED_OS
			#if MD_USD_SALVE
			MDSTimeHandler100US_1(sys_tick_100us);
			#else
			MDMTimeHandler100US(sys_tick_100us);
			#endif
		#endif
	}
}
